package com.cesare.web;

import com.cesare.pojo.Blog;
import com.cesare.pojo.Tag;
import com.cesare.pojo.Type;
import com.cesare.service.BlogService;
import com.cesare.service.TagService;
import com.cesare.service.TypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class IndexController {

    @Autowired
    private BlogService blogService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagService  tagService;

    @GetMapping("/")
    public String index(@RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum, Model model){
        PageHelper.startPage(pageNum,6);
        //获取所有分类
        List<Type> types = typeService.getAllType();
        for(Type type :types){
            type.setBlogs(blogService.getBlogByTypeId(type.getId()));
        }
        PageHelper.startPage(pageNum,6);
        //获取所有标签
        List<Tag> tags = tagService.getAllTag();
        for(Tag tag : tags){
            tag.setBlogs(blogService.getBlogByTagId(tag.getId()));
        }
        PageHelper.startPage(pageNum,6);
        //前台获取博客列表
        List<Blog> blogs = blogService.getIndexBlog();
        PageInfo<Blog> pageInfo = new PageInfo<>(blogs);
        //前台获取推荐博客
        List<Blog> recommendBlogs = blogService.getRecommendBlog();

        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("types",types);
        model.addAttribute("tags",tags);
        model.addAttribute("recommendBlogs",recommendBlogs);
        return "index";
    }

    @GetMapping("/footer/newblog")
    public String newBlogs(Model model){
        PageHelper.startPage(1,3);
        //前台获取推荐博客
        List<Blog> recommendBlogs = blogService.getRecommendBlog();
        model.addAttribute("newblogs",recommendBlogs);
        return "_fragments :: newblogList";
    }

    //博客详情
    @GetMapping("/blog/{id}")
    public String blog(@PathVariable("id") Long id, Model model){
        Blog blog = blogService.getDetailedBlog(id);
        model.addAttribute("blog",blog);
        return "blog";
    }

    @PostMapping("/search")
    public String search(@RequestParam(value = "pageNum",defaultValue = "1",required = false)Integer pageNum,
                         @RequestParam String query,
                         Model model){
        PageHelper.startPage(pageNum,6);
        List<Blog> blogs = blogService.searchIndexBlog(query);
        PageInfo<Blog> pageInfo = new PageInfo<>(blogs);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("query",query);
        return "search";
    }

    @GetMapping("/music")
    public String about() {
        return "music";
    }

    @GetMapping("/message")
    public String message(){
        return "message";
    }

    @GetMapping("/about")
    public String aboutme(){
        return "about";
    }

}
